package com.example.rouroudate.ui.widget

import android.annotation.SuppressLint
import android.content.Context
import android.os.Bundle
import android.util.Log
import android.view.MotionEvent
import android.widget.ListView
import com.example.rouroudate.R
import com.example.rouroudate.ui.gallery.ui.DateListAdapter
import com.example.rouroudate.ui.home.ui.CircleDate
import com.example.rouroudate.util.DisplayUtils
import com.example.rouroudate.util.TimeUtils.DAY
import com.example.rouroudate.util.TimeUtils.MONTH
import com.example.rouroudate.util.TimeUtils.YEAR

open class CurrentDateSetDialog constructor(context: Context) : CommonBottomDialog(context) {

    companion object {
        const val TAG = "CurrentDateSetDialog"
        private const val yearSize = 100
        private const val monthSize = 12
    }

    private var daySize = 30
    private lateinit var yearListView: ListView
    private lateinit var monthListView: ListView
    private lateinit var dayListView: ListView
    private val today = CircleDate()
    lateinit var selectedDay: CircleDate
    private val dayData = ArrayList<String>()
    private var settingDataCallback: (CircleDate) -> Unit = {}

    private val yearData = arrayListOf<Int>().apply {
        for (i in (today.year - yearSize)..today.year) {
            add(i)
        }
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.time_dialog_layout)
        yearListView = findViewById(R.id.yeah_view)!!
        monthListView = findViewById(R.id.month_view)!!
        dayListView = findViewById(R.id.day_view)!!
        initAdapter(yearListView, arrayListOf<String>().apply {
            for (i in (today.year - yearSize)..today.year) {
                add(String.format("%s 年", i))
            }
        }, YEAR)
        initAdapter(monthListView, arrayListOf<String>().apply {
            for (i in 1..monthSize) {
                add(String.format("%s 月", i))
            }
        }, MONTH)
        initDay()
        initAdapter(dayListView, dayData, DAY)
        setRightViewClick {
            val result = selectedDay
            Log.d(TAG, "selectedDay = $result")
            settingDataCallback.invoke(result)
            dismiss()
        }
    }

    override fun onStart() {
        super.onStart()
        Log.d(TAG, "start")
    }

    override fun onRestoreInstanceState(savedInstanceState: Bundle) {
        super.onRestoreInstanceState(savedInstanceState)
        Log.d(TAG, "onRestoreInstanceState")
    }

    fun setCallBack(callback: (CircleDate) -> Unit = {}) {
        settingDataCallback = callback
    }

    private fun initDay() {
        dayData.clear()
        daySize = selectedDay.getDaySize()
        if (selectedDay.day > daySize) selectedDay.day = daySize
        for (i in 1..daySize) {
            dayData.add(String.format("%s 日", i))
        }
    }

    @SuppressLint("ClickableViewAccessibility")
    private fun initAdapter(view: ListView, data: ArrayList<String>, type: Int) {
        view.setOnTouchListener { v, event ->
            when(event.action) {
                MotionEvent.ACTION_DOWN -> v.isNestedScrollingEnabled = true
                MotionEvent.ACTION_UP -> v.isNestedScrollingEnabled = false
                else -> {}
            }
            false
        }
        view.adapter = DateListAdapter(data, context)
        smooth(view, type, data)
        view.setOnScrollListener(DateScrollListener(dateScrollCallback, type))
    }

    private val dateScrollCallback = object : DateScrollCallback {
        override fun onScrollEnd(position: Int, type: Int) {
            when(type) {
                YEAR -> selectedDay.year = yearData[position % yearData.size]
                MONTH -> selectedDay.month = position % monthSize + 1
                else -> selectedDay.day = position % daySize + 1
            }
            if (type != DAY) {
                initDay()
                (dayListView.adapter as DateListAdapter).notifyDataSetChanged()
                smooth(dayListView, DAY, dayData)
            }
        }
    }

    private fun smooth(p0: ListView, type: Int, data: ArrayList<String>) {
        val h = (DisplayUtils.getScreenHeight() * 0.3f).toInt()
        val height = DisplayUtils.dp2px(85f)
        val count = h / height + if (h / height == 0) 0 else 1
        val offset = ((h - height) / 2) % height - height
        val midZero = Int.MAX_VALUE / 2 - (Int.MAX_VALUE / 2 % data.size)
        val offsetNum = 1 + count / 2
        val position = when(type) {
            YEAR -> midZero - today.year + selectedDay.year - offsetNum
            MONTH -> midZero + selectedDay.month - offsetNum
            else -> midZero + selectedDay.day - offsetNum
        }
        p0.setSelectionFromTop(position, offset)
    }
}